/*
 * @Author: NtyAaron
 * @Date: 2021-04-12 11:13:45
 * @LastEditors: NtyAaron
 * @LastEditTime: 2021-05-18 11:48:17
 * @Description: file content
 * @FilePath: \dwq\pages\check\railwayResult\railwayResult.js
 */

import { getRailway } from '../../../api/checkRailway/checkRailway'
import { formatDate } from '../../../utils/util'
import { _t } from '../../../i18n/base'

Page({
  data: {
    railwayList: [],
    routeInfo: {},
    showTips: false,
  },
  // 铁路查询
  async getRailway(e) {
    let { result } = await getRailway(e)
    if (result) {
      if (this.data.language == 3) {
        this.setData({
          railwayList: this.data.railwayList.concat(result),
        })
      } else {
        this.setData({
          railwayList: result,
        })
      }
    } else {
      this.setData({
        showTips: true,
      })
    }
  },
  // 后一天查询
  dayAfterHandle() {
    let dateStr = JSON.parse(this.data.routeInfo.currentDate)
    let date = new Date(dateStr).setDate(new Date(dateStr).getDate() + 1)
    this.setData({
      ['routeInfo.currentDate']: date,
      ['routeInfo.date']: formatDate(new Date(date)),
    })
    this.getRailway(this.data.routeInfo)
  },
  // 前一天查询
  dayBeforeHandle() {
    let dateStr = JSON.parse(this.data.routeInfo.currentDate)
    let date = new Date(dateStr).setDate(new Date(dateStr).getDate() - 1)
    this.setData({
      ['routeInfo.currentDate']: date,
      ['routeInfo.date']: formatDate(new Date(date)),
    })
    this.getRailway(this.data.routeInfo)
  },
  // 分页加载
  scrollEnd() {
    this.setData({
      page: this.data.routeInfo.page + 1,
    })
    this.getRailway(this.data.routeInfo)
  },
  onLoad(e) {
    if (e.info) {
      let info = JSON.parse(e.info)
      let language = wx.getStorageSync('lang')
      this.setData({
        lang: _t(),
        routeInfo: info,
        language,
        ['routeInfo.page']: 1,
        ['routeInfo.size']: 10,
      })
      wx.setNavigationBarTitle({
        title: this.data.lang['铁路查询'],
      })
      this.getRailway(this.data.routeInfo)
    }
  },
})
